package com.example.controller;

import com.example.entity.Orders;
import com.example.service.OrdersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@RequestMapping("/admin")
@Controller
public class Orderscontroller {
    @Resource
    OrdersService ordersService;

    @RequestMapping(value = "/orders",method = {RequestMethod.GET,RequestMethod.POST})
    public String findAllOrders(Model model){
        ArrayList<Orders> orderslist = ordersService.queryAll();
        model.addAttribute("orderslist",orderslist);
        return "ordersList";
    }
    @GetMapping("/delOrders/{id}")
    public String delOrders (@PathVariable("id")int id,Model model){
        int num =ordersService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/orders";
    }
    @GetMapping("/orders/{id}")
    public String getOrdersById(@PathVariable("id") int id, Model model){
        Orders orders =ordersService.findById(id);
        model.addAttribute("orders",orders);
        return "ordersEdit";
    }
    @PostMapping("/editOrders")
    public String addOrders(Orders orders,Model model){
        System.out.println(orders);
        int num = ordersService.update(orders);
        model.addAttribute("msg","订单更新成功");
        return "ordersEdit";
    }
    @GetMapping("/ordersadd")
    public String insertOrders(Orders orders,Model model){
        ArrayList<Orders> ordersArrayList = ordersService.queryAll();
        return "ordersadd";
    }
    @PostMapping("/orders2")
    public String addOrders2(Orders orders,Model model){
        System.out.println(orders);
        int num2 =ordersService.insert(orders);
        return "redirect:orders";
    }

}
